package com.itheima.demo5;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class IODemo16 {
    public static void main(String[] args) {
        File src = new File("a.txt");
        File dest = new File("D:\\");

    }


    public static void toZip(File src,File dest) throws IOException {
        //1.创建压缩流关联压缩包

        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest, "a.zip")));
        //创建ZipEntry对象，表示压缩包里的每一个文件和文件夹
        ZipEntry entry = new ZipEntry("a.txt");
        //将文件放入压缩包中
        zos.putNextEntry(entry);
        //将文件数据写入压缩包中
        FileInputStream fis = new FileInputStream(src);
        int b;
        while ((b = fis.read()) != -1){
            zos.write(b);
        }

        zos.closeEntry();
        zos.close();

    }
}
